package com.example.bawolf.effctivejava.bean;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fanyuwen
 * 2020-03-12 16:18
 */
public class BusinessCustom {

    public static void main(String[] args) {
        for (int i = 1; i < 11; i++) {
            商户业务处理1(i);
        }
        System.out.println("==================下面是面向对象设计重构方案======================");
        for (int i = 1; i < 11; i++) {
            商户业务处理2(i);
        }
    }

    private static final Integer 呷哺呷哺 = 1;
    private static final Integer 绿茶 = 2;
    private static final Integer 五芳斋 = 3;
    private static final Integer 八合里 = 4;
    private static final Integer 乐天 = 5;
    private static final Integer 餐道 = 6;
    private static final Integer 白杨 = 7;
    private static final Integer 鸿翔 = 8;
    private static final Integer 陈文 = 9;
    private static final Integer 梓涵 = 10;

    /**
     * 已知一下商户
     * brandId  商户名   需求
     * 1     呷哺呷哺    执行第二步之后打印 "你好"
     * **************************打印 "欢迎光临"
     * **************************打印 "欢迎再来"
     * 2     绿茶       执行第三步之后打印 "我很绿"
     * 3     五芳斋     执行第二步之后打印 "你好"
     * **************************打印 "欢迎光临"
     * ********************打印 "请品尝我的粽子"
     * *************************打印 "欢迎再来"
     * 4     八合里     执行第一步之后打印 "驻场办公"
     * **********打印完第五步之后添加打印 "执行第六步"
     * 5     乐天       执行第三步之后打印 "我只是测试商户"
     * 6     餐道       执行第二步之后打印 "你好"
     * 7     白杨       没有定制需求
     * 8     鸿翔       没有定制需求
     * 9     陈文       没有定制需求
     * 10    梓涵       不属于该商户的其它商户,在最后一步执行完之后打印 "谢谢"
     *
     * @param brandId 商户id
     */
    private static void 商户业务处理1(int brandId) {
        执行处理步骤1(brandId);
        if (brandId == 八合里) {
            System.out.println("驻场办公");
        }
        执行处理步骤2(brandId);
        if (brandId == 呷哺呷哺 || brandId == 五芳斋 || brandId == 餐道) {
            System.out.println("你好");
            if (brandId != 餐道) {
                System.out.println("欢迎光临");
                if (brandId == 五芳斋) {
                    System.out.println("请品尝我的粽子");
                }
                System.out.println("欢迎再来");
            }
        }
        执行处理步骤3(brandId);
        if (brandId == 绿茶) {
            System.out.println("我很绿");
        }
        if (brandId == 乐天) {
            System.out.println("我只是测试商户");
        }
        执行处理步骤4(brandId);
        执行处理步骤5(brandId);
        if (brandId == 八合里) {
            System.out.println("执行第六步");
        }
        if (brandId != 梓涵) {
            System.out.println("谢谢");
        }
    }

    static void 执行处理步骤1(Integer brandId) {
        System.out.println("商户:" + brandId + " 执行第一步");
    }

    static void 执行处理步骤2(Integer brandId) {
        System.out.println("商户:" + brandId + " 执行第二步");
    }

    static void 执行处理步骤3(Integer brandId) {
        System.out.println("商户:" + brandId + " 执行第三步");
    }

    static void 执行处理步骤4(Integer brandId) {
        System.out.println("商户:" + brandId + " 执行第四步");
    }

    static void 执行处理步骤5(Integer brandId) {
        System.out.println("商户:" + brandId + " 执行第五步");
    }

    private static void 商户业务处理2(int brandId) {
        Business business = Business.accquire(brandId);
        执行处理步骤1(brandId);
        business.afterOneStep();
        执行处理步骤2(brandId);
        business.afterTwoStep();
        执行处理步骤3(brandId);
        business.afterThreeStep();
        执行处理步骤4(brandId);
        business.afterFourStep();
        执行处理步骤5(brandId);
        business.afterFiveStep();
        business.ultimately();
    }

    /**
     * 1.定义商户基类
     */
    static abstract class Business {
        abstract int brandId();
        //2.定义执行方法
        //第一步执行后
        void afterOneStep() {}
        //第二步执行后
        void afterTwoStep() {}
        //第三步执行后
        void afterThreeStep() {}
        //第四步执行后
        void afterFourStep() {}
        //第五步执行后
        void afterFiveStep() {}
        //最后执行
        void ultimately() {System.out.println("谢谢");}
        //-----------------------可以忽略下面的代码-------------------------
        private static Map<Integer, Business> cache = new HashMap<>();
        Business(){cache.put(brandId(), this);}
        static Business accquire(int key){ return cache.getOrDefault(key, DefaultBusiness.DEFAULT_BUSINESS);}
    }
    static class DefaultBusiness extends Business{
        @Override
        int brandId() {return -1;}
        //---------------------------单列--------------------
        private static DefaultBusiness DEFAULT_BUSINESS = new DefaultBusiness();
        DefaultBusiness(){
            if(DEFAULT_BUSINESS != null){
                throw new RuntimeException("我是单例对象");
            }
        }
    }
    /**
     * 打招呼商户
     */
    static abstract class WelComeBusiness extends Business {
        @Override
        final void afterTwoStep() {
            System.out.println("你好");
            consumer();
        }
        void consumer(){}
    }
    /**
     * 消费商户
     */
    static abstract class ConsumerBusiness extends WelComeBusiness{
        @Override
        final void consumer() {
            System.out.println("欢迎光临");
            taste();
            System.out.println("欢迎再来");
        }
        void taste(){}
    }
    static class 呷哺呷哺 extends ConsumerBusiness {
        @Override
        int brandId() {return 1;}
    }
    static class 绿茶 extends Business{
        @Override
        int brandId() {return 2;}
        @Override
        void afterThreeStep() {
            System.out.println("我很绿");
        }
    }
    static class 五芳斋 extends ConsumerBusiness{
        @Override
        int brandId() {return 3;}
        @Override
        void taste() {
            System.out.println("请品尝我的粽子");
        }
    }
    static class 八合里 extends Business{
        @Override
        int brandId() {return 4;}
        @Override
        void afterOneStep() { System.out.println("驻场办公");}
        @Override
        void afterFiveStep() {System.out.println("执行第六步");}
    }
    static class 乐天 extends Business{
        @Override
        int brandId() {return 5;}
        @Override
        void afterThreeStep() {
            System.out.println("我只是测试商户");
        }
    }
    static class 餐道 extends WelComeBusiness{
        @Override
        int brandId() {return 6;}
    }
    static class 梓涵 extends Business{
        @Override
        int brandId() {return 10;}
        @Override
        void ultimately() {}
    }


    static {
        new 呷哺呷哺();
        new 绿茶();
        new 五芳斋();
        new 八合里();
        new 乐天();
        new 餐道();
        new 梓涵();
    }
}